package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.TaskReceiverIndex;
import ge.telasi.tasks.search.Condition;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.Navigator;
import ge.telasi.tasks.ui.search.BooleanField;
import ge.telasi.tasks.ui.search.DateField;
import ge.telasi.tasks.ui.search.Field;
import ge.telasi.tasks.ui.search.SearchPanel;
import ge.telasi.tasks.ui.search.StructureField;
import ge.telasi.tasks.ui.search.TaskPriorityField;
import ge.telasi.tasks.ui.search.TaskStatusField;
import ge.telasi.tasks.ui.search.TextField;
import ge.telasi.tasks.ui.search.UserField;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * Task search panel.
 *
 * @author dimitri
 */
public class TaskSearchPanel extends JPanel {
    private SearchPanel searchPanel;
    private TaskIndexTable table;
    private Navigator navigator;

    public TaskSearchPanel() {
        initComponents();
        addSearchLine();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        add(topComponent(), BorderLayout.NORTH);
        add(centerComponent(), BorderLayout.CENTER);
    }

    private Component topComponent() {
        JPanel panel = new JPanel(new BorderLayout());
        searchPanel = new SearchPanel();
        searchPanel.setFields(getSearchableFields());
        JPanel buttonPanel = new JPanel(new BorderLayout());
        JButton addButton = new JButton();
        JButton searchButton = new JButton("ძებნა");
        addButton.setIcon(UIUtils.getIcon16x16("add.png"));
        addButton.setToolTipText("პირობის დამატება");
        searchButton.setIcon(UIUtils.getIcon16x16("find.png"));
        searchButton.setToolTipText("დავალებების ძებნა");
        addButton.setBorderPainted(false);
        addButton.setPreferredSize(new Dimension(25, addButton.getPreferredSize().height));
        buttonPanel.add(searchButton, BorderLayout.WEST);
        buttonPanel.add(addButton, BorderLayout.EAST);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addSearchLine();
            }
        });
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                List<Condition> conditions = searchPanel.getConditions();
                table.setPage(0);
                table.clearData();
                navigator.reset();
                if (conditions == null || conditions.isEmpty()) {
                    UIUtils.showMessage(TaskSearchPanel.this, "განსაზღვრეთ ძებნის პარამეტრები.");
                } else {
                    table.setConditions(conditions);
                    table.refresh();
                }
            }
        });
        panel.add(searchPanel, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        return panel;
    }

    private Component centerComponent() {
        JPanel panel = new JPanel(new BorderLayout());
        table = new TaskIndexTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        panel.add(new JScrollPane(table));
        navigator = new Navigator();
        navigator.setTable(table);
        navigator.reset();
        panel.add(navigator, BorderLayout.SOUTH);
        return panel;
    }

    private Field[] getSearchableFields() {
        Field number = new TextField("taskSearch.number", "ნომერი");
        Field cancNumber = new TextField("taskSearch.canc_number", "კანც. №");
        Field subject = new TextField("taskSearch.subject", "სათაური");
        Field due_date = new DateField("taskSearch.dueDate", "ბოლო თარიღი");
        Field created = new DateField("taskSearch.created", "შექმნის თარიღი");
        Field hasAttachment = new BooleanField("taskSearch.attachment", "აქვს ფაილები");
        Field isNew = new BooleanField("taskSearch.newTask", "ახალი");
        Field status = new TaskStatusField();
        Field priority = new TaskPriorityField();
        Field sentTo = new UserField("taskSearch.receiver_user", "ვინ მიიღო?");
        Field owner = new UserField("taskSearch.owner", "მფლობელი");
        Field preparedBy = new UserField("taskSearch.preparedBy", "მოამზადა");
        Field structure = new StructureField("taskSearch.structure", "სტრუქტ. ერთეული");
        Field[] fields = new Field[] {number, cancNumber, subject, sentTo, owner,
            preparedBy, structure, due_date, created, status, priority, isNew,
            hasAttachment};
        return fields;
    }

    private void addSearchLine() {
        searchPanel.addLine();
    }

    public TaskReceiverIndex getSelection() {
        return table.getSelection();
    }

    public List<TaskReceiverIndex> getSelections() {
        return table.getSelections();
    }

    public void openTask() {
        table.openTask();
    }

}
